/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.proyectofinal.ramas.controller;

import com.proyectofinal.ramas.domain.User;
import com.proyectofinal.ramas.domain.UserRole;
import com.proyectofinal.ramas.domain.UserRolePK;
import com.proyectofinal.ramas.forms.AddUserForm;
import com.proyectofinal.ramas.service.CompanyService;
import com.proyectofinal.ramas.service.RoleService;
import com.proyectofinal.ramas.service.UserRoleService;
import com.proyectofinal.ramas.service.UserRoleStatusService;
import com.proyectofinal.ramas.service.UserService;
import com.proyectofinal.ramas.service.UserStatusService;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

/**
 *
 * @author nicolas
 */
@Controller
@SessionAttributes("currentUserRole")
public class UserController {
    
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserStatusService userStatusService;
    @Autowired
    private UserRoleStatusService userRoleStatusService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private CompanyService companyService;
    
    @RequestMapping(value = "/addUser", method = RequestMethod.GET)
    public String addClient(Map model) {
        model.put("addUserForm", new AddUserForm());
        model.put("roles",roleService.getAll());

        return "addUser";
    }
    
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public String addClientValidation(@ModelAttribute("currentUserRole") UserRole currentUserRole,
                                        @Valid @ModelAttribute("addUserForm") AddUserForm addUserForm,
                                        BindingResult result, 
                                        Map model) throws NoSuchAlgorithmException {
       if (result.hasErrors()) {  
            model.put("roles", roleService.getAll());
            return "addUser";  
      }  
       
      
        User user = new User();
        user.setUserName(addUserForm.getUserName());
        user.setPassword(addUserForm.getPassword());
        user.setFirstName(addUserForm.getFirstName());
        user.setLastName(addUserForm.getLastName());
        user.setEmail(addUserForm.getEmail());
        user.setUserStatus(userStatusService.get(new Byte("1"))); //Le seteo el estado 'Alta' por defecto
        user.setLoadDate(new Timestamp(new Date().getTime()));
        userService.add(user);

        UserRolePK userRolePK = new UserRolePK(user, roleService.get(addUserForm.getRoleID()),currentUserRole.getUserRolePK().getCompany()) ;

        UserRole userRole = new UserRole();
        userRole.setUserRolePK(userRolePK);
        userRole.setUserRoleStatus(userRoleStatusService.get(new Byte("1"))); //Le seteo el estado 'Alta' por defecto
        userRole.setLoadDate(new Timestamp(new Date().getTime()));
        userRoleService.add(userRole);
//       
//       
       return "addUserConfirmation";  
      }  
    
    

    @RequestMapping(value = "/addUserConfirmation", method = RequestMethod.POST)
    public String addClientConfirmation(Model model,
            @ModelAttribute("user") User user,
            @ModelAttribute("currentUserRole") UserRole currentUserRole,
            @RequestParam("roleID") Integer roleID) {
        
        user.setUserStatus(userStatusService.get(new Byte("1"))); //Le seteo el estado 'Alta' por defecto
        user.setLoadDate(new Timestamp(new Date().getTime()));
        userService.add(user);
        
        UserRolePK userRolePK = new UserRolePK(user, roleService.get(roleID), currentUserRole.getUserRolePK().getCompany());
        
        UserRole userRole = new UserRole();
        userRole.setUserRolePK(userRolePK);
        userRole.setUserRoleStatus(userRoleStatusService.get(new Byte("1"))); //Le seteo el estado 'Alta' por defecto
        userRole.setLoadDate(new Timestamp(new Date().getTime()));
        userRoleService.add(userRole);

        return "addUserConfirmation";
    }
    
}
